﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TosObserver;

namespace Tos2Google
{
    public class FullData // the thing we feed to decision makers
    {
        private static Dictionary<TOSTopic, int> syncFields = null; // int == col index (negative if one-time-sync)

        public static Dictionary<TOSTopic, int> SyncFields
        {
            get
            {
                if (syncFields == null)
                {
                    syncFields = new Dictionary<TOSTopic, int>();

                    // one-time-sync GROUP1 (price change daily, has nothing to do with market hour )
                    syncFields.Add(TOSTopic.Low52, -2);
                    syncFields.Add(TOSTopic.Close, -3);
                    // THIS SEEMS NOT TO FLOW THROUGH... syncFields.Add(TOSTopic.MarketCap, -4);
                    syncFields.Add(TOSTopic.EPS, -5);
                    syncFields.Add(TOSTopic.PE, -6);
                    // volumeavg ... ?

                    // one-time-sync GROUP2 (price change daily, related do market hour, only update once in the beginning of market hour)
                    syncFields.Add(TOSTopic.Open, -7);

                    // continuous sync
                    syncFields.Add(TOSTopic.Last, 8);
                    syncFields.Add(TOSTopic.Volume, 9);
                    syncFields.Add(TOSTopic.Low, 10);
                    syncFields.Add(TOSTopic.High, 11);
                    syncFields.Add(TOSTopic.NetChange, 12);
                    syncFields.Add(TOSTopic.PercentChange, 13);
                    syncFields.Add(TOSTopic.MarketCap, 14);
                }
                return syncFields;
            }
        }


        private decimal[] Data = new decimal[12];

        private int TOSTopic_to_Index(TOSTopic tosTopic)
        {
            // try to put more frequent ones first (== less IFs to process in the big picture)
            if (tosTopic.Name == "LAST")
                return 0;
            if (tosTopic.Name == "NET_CHANGE")
                return 1;
            if (tosTopic.Name == "PERCENT_CHANGE")
                return 10;
            if (tosTopic.Name == "VOLUME")
                return 2;
            if (tosTopic.Name == "LOW")
                return 3;
            if (tosTopic.Name == "HIGH")
                return 4;
            if (tosTopic.Name == "OPEN")
                return 5;
            if (tosTopic.Name == "CLOSE")
                return 6;
            if (tosTopic.Name == "52LOW")
                return 7;
            if (tosTopic.Name == "EPS")
                return 9;
            if (tosTopic.Name == "PE")
                return 8;
            if (tosTopic.Name == "MARKET_CAP")
                return 11;
            return -1;
        }

        public decimal Low52 { get { return Data[7]; } }
        public decimal Close { get { return Data[6]; } }
        public decimal PercentChange { get { return Data[10]; } }
        public decimal EPS { get { return Data[9]; } }
        public decimal PE { get { return Data[8]; } }
        public decimal Open { get { return Data[5]; } }
        public decimal Last { get { return Data[0]; } }
        public decimal Volume { get { return Data[2]; } }
        public decimal Low { get { return Data[3]; } }
        public decimal High { get { return Data[4]; } }
        public decimal NetChange { get { return Data[1]; } }
        public decimal MarketCap { get { return Data[11]; } }

        public Dictionary<int, bool> AlreadyFilled = new Dictionary<int, bool>();
        public bool AllFilled { get { return AlreadyFilled.Count == SyncFields.Count; } }

        internal void Fill(TOSTopic tosTopic, decimal value)
        {
            int index = TOSTopic_to_Index(tosTopic);
            if (index == -1)
                return; // irrelevant
            Data[index] = value;

            if (AlreadyFilled.ContainsKey(index))
                return;
            AlreadyFilled.Add(index, true);
        }
    }
}
